package com.demo.rigistry.filter;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;

//restful风格的curd操作
//post put get delete
@Configuration
public class FilterConfig {

	// 注册restful请求过滤器
	@Bean
	public FilterRegistrationBean hiddenHttpMethodFilter() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
		registrationBean.setFilter(hiddenHttpMethodFilter);
		List<String> urlPatterns = new ArrayList<String>();
		urlPatterns.add("/*");
		registrationBean.setUrlPatterns(urlPatterns);
		return registrationBean;

	}

	// 注册自定义过滤器
	// @Bean
	// public FilterRegistrationBean loginFilter() {
	// FilterRegistrationBean registrationBean = new FilterRegistrationBean();
	// LoginFilter weChatFilter = new LoginFilter();
	// registrationBean.setFilter(weChatFilter);
	// List<String> urlPatterns = new ArrayList<String>();
	// urlPatterns.add("/*");
	// registrationBean.setUrlPatterns(urlPatterns);
	// return registrationBean;
	// }

	// 注册编码过滤器
	@Bean
	public Filter characterEncodingFilter() {
		// 编码过滤器
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		characterEncodingFilter.setForceEncoding(true);
		return characterEncodingFilter;
	}
}
